<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.org/ui"
	xmlns:sec="http://www.springframework.org/security/facelets/tags">
	
<h:body>
	<ui:composition template="/templates/templateCRUD.xhtml">
		<ui:param name="tituloCadastro" value="Cadastro de Funcionários" />
		<ui:param name="tituloPesquisa" value="Pesquisa de Funcionários" />
		<ui:param name="tituloGrid" value="Lista de Funcionários" />
		<ui:param name="mbean" value="${funcionarioMB}" />
		<ui:define name="cadastro">
			<p:panel>
				<h:panelGrid columns="4" columnClasses="coluna-direita, coluna-esquerda, coluna-direita, coluna-esquerda">
					<h:outputText value="Clínica: "/>
					<h:panelGroup>
						<p:selectOneMenu value="#{mbean.bean.empresa}" label="Clínica" id="empresaCadastro" required="true" disabled="#{not mbean.inclusao}">
							<sec:ifAllGranted roles="ROLE_SYSADMIN">
								<f:selectItem itemLabel="#{msg.opcaoSelecionarCombo}" itemValue="#{null}"/>
							</sec:ifAllGranted>
							<f:selectItems value="#{mbean.empresas}" var="empresa" itemLabel="#{empresa.nomeFantasia}"/>
							<f:converter converterId="empresaConverter"/>
							<p:ajax event="change" update="@form" listener="#{mbean.selecionarEmpresa}"/>	
						</p:selectOneMenu>
						<p:message for="empresaCadastro" showDetail="true" showSummary="true"/>
					</h:panelGroup>
					
					<h:outputText value="Email: " />
					<h:panelGroup>
						<p:inputText required="true" id="emailCadastro" validator="emailValidator" size="60" 
							value="#{mbean.bean.usuario.email}" label="Email" readonly="#{not mbean.inclusao}">
							<p:ajax event="blur" update="@form" listener="#{mbean.pesquisarUsuario}" global="false" process="@parent, empresaCadastro"/>	
						</p:inputText>
						<p:message for="emailCadastro" showDetail="true" showSummary="true"/>
					</h:panelGroup>
					
					<h:outputText value="Login: " />
					<h:inputText value="#{mbean.bean.usuario.login}" disabled="true" size="70"/>
					
					<h:outputText value="Nome da Pessoa: " />
					<h:inputText value="#{mbean.bean.usuario.nome}" disabled="true" size="60"/>
					
					<h:outputText value="Data Registro: " />
					<h:inputText value="#{mbean.bean.usuario.dataCriacao}" disabled="true" size="20">
						<f:convertDateTime pattern="dd/MM/yyyy"/>
					</h:inputText>
					
				
					<h:outputText value="Ativar Funcionário: " />
					<p:selectOneRadio value="#{mbean.bean.ativo}" disabled="#{mbean.readOnly}">
						<f:selectItem itemLabel="Sim" itemValue="true"/>
						<f:selectItem itemLabel="Não" itemValue="false"/>  
					</p:selectOneRadio>

						
					<h:outputText value="Perfil: "/>
					<h:panelGrid columns="2" cellspacing="4">
						<p:selectOneMenu value="#{mbean.perfil}" disabled="#{mbean.readOnly}" id="comboPerfil" label="Perfil">
							<f:selectItems value="#{mbean.perfis}" var="perfil" itemLabel="#{perfil.descricao}"/>
							<f:converter converterId="perfilConverter"/>
						</p:selectOneMenu>
						
						<p:commandButton value="Adicionar" actionListener="#{mbean.adicionarPerfil}" update="gridPerfis, comboPerfil" 
							disabled="#{mbean.readOnly}" immediate="false" process="@this, comboPerfil"/>
					</h:panelGrid>
				</h:panelGrid>
							
				<br/>
				
				<p:panel header="Perfis do usuário">
					<p:dataTable var="registro" value="#{mbean.bean.perfis}" emptyMessage="" id="gridPerfis">
						<p:column>
							<f:facet name="header">
								<h:outputText value="Descrição"/>
							</f:facet>
							<h:outputText value="#{registro.descricao}"/>
						</p:column>
						<p:column>
							<f:facet name="header">
								<h:outputText value="Nome"/>
							</f:facet>
							<h:outputText value="#{registro.nome}"/>
						</p:column>
			          	<p:column style="width:32px">  
				             <p:commandButton icon="ui-icon-trash" action="#{mbean.excluirPerfil(registro)}" 
				             	disabled="#{mbean.readOnly}" immediate="true" oncomplete="atualizarGrid();"/>
						</p:column> 
					</p:dataTable>
				</p:panel>
				<p:remoteCommand update="gridPerfis" name="atualizarGrid"/>
			</p:panel>
		</ui:define>

		<ui:define name="pesquisa">
			<p:focus for="emailPesquisa"/>
			<h:panelGrid columns="4" cellpadding="4">
				<h:outputText value="Email: " />
				<p:inputText value="#{mbean.beanSearch.usuario.email}" size="54" maxlength="50" id="emailPesquisa"/>
				
				<h:outputText value="Clínica: "/>
				<p:selectOneMenu value="#{mbean.beanSearch.empresa}">
					<sec:ifAllGranted roles="ROLE_SYSADMIN">
						<f:selectItem itemLabel="#{msg.opcaoSelecionarCombo}" itemValue="#{null}"/>
					</sec:ifAllGranted>
					<f:selectItems value="#{mbean.empresas}" var="empresa" itemLabel="#{empresa.nomeFantasia}"/>
					<f:converter converterId="empresaConverter"/>
				</p:selectOneMenu>
			</h:panelGrid>
		</ui:define>
		
		<ui:define name="gridPesquisa">
			<p:column>
				<f:facet name="header">
					<h:outputText value="Email"/>
				</f:facet>
				<h:outputText value="#{registro.usuario.email}"/>
			</p:column>
			
			<p:column>
				<f:facet name="header">
					<h:outputText value="Login"/>
				</f:facet>
				<h:outputText value="#{registro.usuario.login}"/>
			</p:column>
			
			<p:column>
				<f:facet name="header">
					<h:outputText value="Nome"/>
				</f:facet>
				<h:outputText value="#{registro.usuario.nome}"/>
			</p:column>
			
			<p:column>
				<f:facet name="header">
					<h:outputText value="Clínica"/>
				</f:facet>
				<h:outputText value="#{registro.empresa.nomeFantasia}"/>
			</p:column>
			
			<p:column>
				<f:facet name="header">
					<h:outputText value="Ativo"/>
				</f:facet>
				<h:outputText value="#{registro.ativo ? 'SIM' : '-'}"/>
			</p:column>
			
			<sec:ifAllGranted roles="ROLE_SYSADMIN">
			<p:column>
				<f:facet name="header">
					<h:outputText value="Adminstrador do Sistema"/>
				</f:facet>
				<h:outputText value="#{registro.perfilAdministradorSistema ? 'SIM' : '-'}"/>
			</p:column>
			</sec:ifAllGranted>
			
			<p:column>
				<f:facet name="header">
					<h:outputText value="Adminstrador"/>
				</f:facet>
				<h:outputText value="#{registro.perfilAdministrador ? 'SIM' : '-'}"/>
			</p:column>
			
			<p:column>
				<f:facet name="header">
					<h:outputText value="Médico"/>
				</f:facet>
				<h:outputText value="#{registro.perfilMedico ? 'SIM' : '-'}"/>
			</p:column>
			
			<p:column>
				<f:facet name="header">
					<h:outputText value="Secretária"/>
				</f:facet>
				<h:outputText value="#{registro.perfilSecretaria ? 'SIM' : '-'}"/>
			</p:column>
			
			<p:column>
				<f:facet name="header">
					<h:outputText value="Data de Associação"/>
				</f:facet>
				<h:outputText value="#{registro.dataAssociacao}">
					<f:convertDateTime pattern="dd/MM/yyyy"/>
				</h:outputText>
			</p:column>
		</ui:define>
	</ui:composition>
</h:body>
</html>
